﻿using System.Threading.Tasks;
using UnityEngine;

public class TestAsync1 : MonoBehaviour
{
    //void Start()
    //{
    //    ExecuteAsync();//新建一个线程执行这个函数,不阻塞主线程,立即执行下面
    //    Debug.Log("Start : " + System.DateTime.Now.Ticks);
    //}

    async void Start()
    {
        Debug.Log("Start : " + System.DateTime.Now.Ticks);

        var t = await ExecuteAsyncWithResult();

        Debug.Log("Done : " + System.DateTime.Now.Ticks);
    }

    async void ExecuteAsync()
    {
        await Task.Run(() =>
        {
            System.Threading.Thread.Sleep(2000);

            Debug.Log("Task End : " + System.DateTime.Now.Ticks);
        });

        Debug.Log("Func End : " + System.DateTime.Now.Ticks);
    }

    async Task<string> ExecuteAsyncWithResult()
    {
        //await Task.Run(() =>
        //{
        //    System.Threading.Thread.Sleep(4000);

        //    Debug.Log("Task End : " + System.DateTime.Now.Ticks);
        //});

        await Task.Run(async () =>
        {
            await Task.Delay(4000);

            Debug.Log("Task End : " + System.DateTime.Now.Ticks);
        });

        Debug.Log("Func End : " + System.DateTime.Now.Ticks);

        return "I have finished my work.";
    }
}
